// pages/sign/issue/issue.js
var dateTimePicker = require('../../../../utils/dateTimePicker.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    startDateTime: null,
    endDateTime: null,
    startDateTimeArray: null,
    endDateTimeArray: null,
    startTime: null,
    endTime: null,
    startYear: 2017,
    endYear: 2050,

    address: null,
    latitude: null,
    longitude: null,

    projectType: 0,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if (options.type == 1 || options.type == 2 || options.type == 3) {
      this.setData({
        projectType: options.type
      })
      if (options.type == 1) {
        wx.setNavigationBarTitle({
          title: '创建课堂'
        })
      }
      if (options.type == 2) {
        wx.setNavigationBarTitle({
          title: '创建会议'
        })
      }
      if (options.type == 3) {
        wx.setNavigationBarTitle({
          title: '创建会议'
        })
      }
    }
    //this.initSignDateTime();
  },



  //发布活动按钮
  formSubmit: function (res) {
    var that = this
    var formValue = res.detail.value
    console.log(res)

    //判断是否选择了地址
    if (this.data.latitude == null || this.data.longitude == null) {
      wx.showToast({
        title: '请选择地点',
        icon: 'none'
      })
      return;
    }
    var formData = res.detail.value
    // formData.startTime = this.data.startTime
    // formData.endTime = this.data.endTime
    formData.latitude = this.data.latitude
    formData.longitude = this.data.longitude
    formData.type = this.data.projectType
    // formData.address = this.data.address
    console.log(formData)

    wx.request({
      url: app.host + '/project/info',
      method: 'post',
      header: {
        accessToken: app.accessToken
      },
      data: formData,
      complete: function (res) {
        console.log(res)
        if (res.statusCode == 200) {
          if (res.data.code == 0) {
            wx.redirectTo({
              url: '../success/success?projectId='+res.data.data.id,
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        } else {
          wx.showToast({
            title: '无法连接服务器',
            icon: 'none'
          })
        }
      }
    })
  },

  //打开地图选择位置
  moveToLocation: function () {
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        console.log(res);
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
          address: res.name
        })
      },
      fail: function (err) {
        console.log(err)
      }
    });
  },






  /**签到时间部分**/
  //初始化签到时间
  initSignDateTime() {
    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    var lastArray = obj.dateTimeArray.pop();
    var lastTime = obj.dateTime.pop();

    var endTime = obj.dateTimeArray[0][obj.dateTime[0]] + "-"
      + obj.dateTimeArray[1][obj.dateTime[1]] + "-"
      + obj.dateTimeArray[2][obj.dateTime[2]] + " "
      + obj.dateTimeArray[3][obj.dateTime[3]] + ":"
      + obj.dateTimeArray[4][obj.dateTime[4]];
    var startTime = endTime

    this.setData({
      startDateTimeArray: obj.dateTimeArray,
      endDateTimeArray: obj.dateTimeArray,
      startDateTime: obj.dateTime,
      endDateTime: obj.dateTime,
      startTime: startTime,
      endTime: endTime
    });
    //

  },
  changeStartDateTime(e) {
    console.log("changeStartDateTime")
    console.log(e.detail.value)
    var that = this
    var dateTimeArray = this.data.startDateTimeArray
    var startTime = dateTimeArray[0][e.detail.value[0]] + "-"
      + dateTimeArray[1][e.detail.value[1]] + "-"
      + dateTimeArray[2][e.detail.value[2]] + " "
      + dateTimeArray[3][e.detail.value[3]] + ":"
      + dateTimeArray[4][e.detail.value[4]];
    this.setData({
      startDateTime: e.detail.value,
      startTime: startTime
    });
  },

  changeStartDateTimeColumn(e) {
    console.log("changeStartDateTimeColumn")
    var arr = this.data.startDateTime;
    var dateArr = this.data.startDateTimeArray;
    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
    this.setData({
      startDateTimeArray: dateArr
    });
  },

  changeEndDateTime(e) {
    console.log("changeEndDateTime")
    var dateTimeArray = this.data.endDateTimeArray
    var endTime = dateTimeArray[0][e.detail.value[0]] + "-"
      + dateTimeArray[1][e.detail.value[1]] + "-"
      + dateTimeArray[2][e.detail.value[2]] + " "
      + dateTimeArray[3][e.detail.value[3]] + ":"
      + dateTimeArray[4][e.detail.value[4]];
    this.setData({
      endDateTime: e.detail.value,
      endTime: endTime
    });
  },

  changeEndDateTimeColumn(e) {
    console.log("changeEndDateTimeColumn")
    var arr = this.data.endDateTime;
    var dateArr = this.data.endDateTimeArray;
    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
    this.setData({
      endDateTimeArray: dateArr
    });
  }


})